package Daoimpl;

import Dao.Dao_mysql;
import domain.Entity_students;
import jdbc_util.Conn_Close;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Dao_mysqlImpl implements Dao_mysql {
    public List<Entity_students> findAll() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<Entity_students> entity_students=new ArrayList<Entity_students>();
        try {
             connection = Conn_Close.getConnection();
             preparedStatement = connection.prepareStatement("SELECT * from students");
             resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                Entity_students es=new Entity_students();
                es.setAge(resultSet.getInt(3));
                es.setId(resultSet.getInt(1));
                es.setName(resultSet.getString(2));
                es.setSex(resultSet.getString(4));
                entity_students.add(es);
            }

        }  catch (SQLException e) {
            e.printStackTrace();
        } finally {
           Conn_Close.close(connection,preparedStatement,resultSet);
        }
        if (entity_students==null){
            return  null;
    }
        else
        return entity_students;

    }
}
